#ifndef LINKQUALITY_H
#define LINKQUALITY_H

#include <QDialog>
#include <QThread>
#include <QString>
#include <QReadWriteLock>
#include <QFile>
#include <QTextStream>

class QLabel;
class QLineEdit;
class QPushButton;
class QUdpSocket;

class updateGPS : public QThread
{
    Q_OBJECT

public:
    updateGPS()
    {
        stopped = false;
    }
    virtual ~updateGPS() {}
    void stop();

signals:
    void updateData(QString time, QString longtitude, QString latitude);

private:
    int openPort();

protected:
    void run();

private:
    bool stopped;
};

class updateRemoteGPS : public QThread
{
    Q_OBJECT

public:
    updateRemoteGPS();
    virtual ~updateRemoteGPS();

signals:
    void updateRemoteData(QByteArray data);

private slots:
    void processPendingDatagrams();

private:
    QUdpSocket *receiveSocket;
};

class LinkQuality : public QDialog
{
    Q_OBJECT

public:
    LinkQuality(QWidget *parent = 0);

private slots:
    void updateGPSData(QString time, QString longtitude, QString latitude);
    void updateRemoteGPSData(QByteArray data);
    void localFinish();
    void remoteFinish();
    void startOrStopThreads();
    void send();

protected:
    void closeEvent(QCloseEvent *);
    void timerEvent(QTimerEvent *);

private:
    void loadGUI();

private:
    QLabel *ipLabel;
    QLabel *portLabel;
    QLabel *localeTimeLabel;
    QLabel *localeLongitudeLabel;
    QLabel *localeLatitudeLabel;
    QLabel *remoteTimeLabel;
    QLabel *remoteLongitudeLabel;
    QLabel *remoteLatitudeLabel;
    QLabel *packetLengthLabel;
    QLabel *countLabel;

    QLineEdit *ipEdit;
    QLineEdit *portEdit;
    QLineEdit *localeTimeEdit;
    QLineEdit *localeLongitudeEdit;
    QLineEdit *localeLatitudeEdit;
    QLineEdit *remoteTimeEdit;
    QLineEdit *remoteLongitudeEdit;
    QLineEdit *remoteLatitudeEdit;
    QLineEdit *packetLengthEdit;
    QLineEdit *countEdit;

    QPushButton *startButton;
    QPushButton *closeButton;

    QString time;
    QString longitude;
    QString latitude;

    updateGPS *localThread;
    updateRemoteGPS *remoteThread;

    bool threadsIsRunning;

    int localTimer;
    int remoteTimer;

    QReadWriteLock fileLock;
    QReadWriteLock gpsLock;

    QFile *file;
    QTextStream *logFile;
};

#endif // LINKQUALITY_H
